use serde::{Serialize, Serializer};
use rusqlite::Error as SqliteError;
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
  #[error("Connect Error")]
  ConnectionError(),
  #[error("Database Open Error: {0}")]
  DatabaseOpenError(String),
  #[error("Database Close Error: {0}")]
  DatabaseCloseError(String),
  #[error("Database Query Error: {0}")]
  DatabaseQueryError(String),
  #[error("Database Update Error: {0}")]
  DatabaseUpdateError(String),
  #[error("Database Delete Error: {0}")]
  DatabaseDeleteError(String),
  #[error("Database Insert Error: {0}")]
  DatabaseInsertError(String),
  #[error("Database Execute Error: {0}")]
  DatabaseExecuteError(String),
}

impl Serialize for Error {
  fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
  where
      S: Serializer,
  {
    serializer.serialize_str(self.to_string().as_ref())
  }
}

impl From<SqliteError> for Error {
  fn from(error: SqliteError) -> Self {
    match error {
      SqliteError::InvalidQuery => Error::DatabaseQueryError("Invalid query".to_string()),
      // 处理其他 rusqlite::Error 变体
      _ => Error::DatabaseQueryError(error.to_string()),
    }
  }
}
